<?php
namespace app\attendance\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Upload extends Command {

    protected function configure() {
        $this->setName('upload')->setDescription('upload image ');
    }

    protected function execute(Input $input, Output $output) {

        $queueName = 'attendance.cloudupload';
        $exchangeName = 'attendance';
        $routeKey = 'cloudupload';
        $connection = new \AMQPConnection(
                ['host' => '172.16.101.175',
            'port' => '5672',
            'vhost' => '/',
            'login' => 'guest',
            'password' => 'guest']);
        $connection->connect() or die("Cannot connect to the broker!\n");
        $channel = new \AMQPChannel($connection);
        $exchange = new \AMQPExchange($channel);
        $exchange->setName($exchangeName);
        $exchange->setType(AMQP_EX_TYPE_DIRECT);
        $exchange->setFlags(AMQP_DURABLE);
        $exchange->declareExchange();
        $queue = new \AMQPQueue($channel);
        $queue->setName($queueName);
        $queue->setFlags(AMQP_DURABLE);
        $queue->declareQueue();

        $queue->bind($exchangeName, $routeKey);
        while (TRUE) {
            $msg = $queue->get(AMQP_AUTOACK);
            if (!$msg) {
                continue;
            }
            $msg = json_decode($msg->getBody());
            
            if (!$msg->image) {
                continue;
            }
            $res = \Qiniu::instance()->uploadBase64Img($msg->image);
            $res = json_decode($res);
            $adress = 'http://opol1n8x0.bkt.clouddn.com/' . $res->key;
            $id = $msg->id;
            $result = Db::connect("db_config_attendance")->name('violation')->where(['id' => $id])->update(['currentImg' => $adress]);
        }
    }
}


